package game;

import java.util.Scanner;

public class Engine {
	private Scanner keyboard = new Scanner(System.in);
	private int diceSum;
	private boolean sixesPreviouslyThrown = false;
	
	public boolean throwDice(Player player, Dice dice, int currentPlayer) {
		keyboard.nextLine();
		diceSum = dice.getSum();
		player.addPoints(currentPlayer, diceSum);
		
		System.out.println(diceSum + " thrown!");
		System.out.println("Player "+ currentPlayer +" now has a score of " + player.getScore(currentPlayer) + "!\n");
		
		if (dice.getEqual()) {
			return this.equalDiceThrown(player, dice, currentPlayer);
		}
		sixesPreviouslyThrown = false;
		return false;
	}
	
	private boolean equalDiceThrown(Player player, Dice dice, int currentPlayer) {
		if (dice.getOnes()) {
			this.ones(player, currentPlayer);
			sixesPreviouslyThrown = false;
			
			System.out.println("Equal pair of dice thrown! Press enter to throw again");
			return this.throwDice(player, dice, currentPlayer);
		}
		
		else if (player.getScore(currentPlayer) >= 40) {
			sixesPreviouslyThrown = false;
			System.out.println(currentPlayer + " threw two equal dice with a score greater than or equal to 40, and won the game!");
			return true;
		}
		
		else if (dice.getSixes()) {
			if (sixesPreviouslyThrown) {
				System.out.println("Two sixes thrown two times in a row! Game won by player " + currentPlayer);
				return true;
			}
			else {
				sixesPreviouslyThrown = true;
				System.out.println("Equal pair of dice thrown! Press enter to throw again");
				return this.throwDice(player, dice, currentPlayer);
			}
		}
		
		sixesPreviouslyThrown = false;
		System.out.println("Equal pair of dice thrown! Press enter to throw again");
		return this.throwDice(player, dice, currentPlayer);
	}
	
	private void ones(Player player, int currentPlayer) {
		System.out.println("Two ones thrown, score reset!");
		player.setZero(currentPlayer);
	}
}